package io.renren.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.Result;
import io.renren.controller.im.TencentCloudImUtil;
import io.renren.dto.ImResult;
import io.renren.entity.ImchatbillEntity;
import io.renren.service.ImchatbillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/wnapp")
@Api(tags = "腾讯IM实时对话")
public class TXController {

    @Autowired
    private TencentCloudImUtil tencentCloudImUtil;

    /**
     * 查询单聊消息
     *
     * @param fromUserId 发送方用户id
     * @param toUserId   接收方用户id
     * @param maxCnt     查询条数
     * @param startTime  起始时间（单位：秒）
     * @param endTime    结束时间（单位：秒）
     * @param lastMsgKey 最后一条消息的 MsgKey  可以为null
     * @return 单聊消息列表
     */
    @GetMapping("/testSendIMMsg")
    @ApiOperation("查询单聊消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fromUserId", value = "发送方用户id", dataType = "String", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "toUserId", value = "接收方用户id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "maxCnt", value = "查询条数", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "startTime", value = "起始时间（单位：秒）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "endTime", value = "结束时间（单位：秒）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "lastMsgKey", value = "最后一条消息的 MsgKey  可以为null", dataType = "String", dataTypeClass = String.class)
    })
    public String testSendIMMsg(String fromUserId, String toUserId, Integer maxCnt, Long startTime, Long
            endTime, String lastMsgKey) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(new Date());
        cale.set(Calendar.HOUR_OF_DAY, 0);
        cale.set(Calendar.MINUTE, 0);
        cale.set(Calendar.SECOND, 0);
        //        Date beginTime = cale.getTime();
        String s = tencentCloudImUtil.adminGetRoamMsg(fromUserId, toUserId, maxCnt, startTime, endTime, lastMsgKey);
        return s;
    }


    /**
     * 查询群聊消息
     */
    //    @GetMapping("/groupSendIMMsg")
    //    @ApiOperation("查询单聊消息")
    //    @ApiImplicitParams({
    //            @ApiImplicitParam(name = "groupId", value = "群id", dataType = "String", dataTypeClass = Integer.class),
    //            @ApiImplicitParam(name = "msgNumber", value = "msgNumber", dataType = "String", dataTypeClass = String.class),
    //            @ApiImplicitParam(name = "msgSeq", value = "msgSeq", dataType = "Integer", dataTypeClass = Integer.class),
    //    })
    //    public ImResult groupSendIMMsg(String groupId, Integer msgNumber, Integer msgSeq) {
    //        ImResult s = tencentCloudImUtil.adminGetQunRoamMsg(groupId, msgNumber, msgSeq);
    //        return s;
    //    }


    @Autowired
    private ImchatbillService imchatbillService;

    @GetMapping("/groupSendIMMsgPage")
    @ApiOperation("查询单聊消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群id", dataType = "String", dataTypeClass = Integer.class)
    })
    public Result groupSendIMMsgPage(String keywords) {
        List<ImchatbillEntity> s = imchatbillService.getListByGroupIdPage(keywords);
        return new Result().ok(s);
    }


    @GetMapping("/groupSendIMMsg")
    @ApiOperation("查询单聊消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群id", dataType = "String", dataTypeClass = Integer.class)
    })
    public Result groupSendIMMsg(String groupId) {
        List<ImResult> s = imchatbillService.getListByGroupId(groupId);
        return new Result().ok(s);
    }


    @GetMapping("/disbandGroup")
    @ApiOperation("解散群")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群 id", dataType = "String", dataTypeClass = String.class)
    })
    public Result disbandGroup(String groupId) {
        // 保存解散群信息
        int i = imchatbillService.saveBill(groupId);
        if (i > 0) {
            String result = tencentCloudImUtil.disbandGroup(groupId);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if ("OK".equals(jsonObject.getString("ActionStatus"))) {
                return new Result().ok("解散成功");
            }
            return new Result().error(jsonObject.getString("ErrorInfo"));
        }
        return new Result().error("解散不成功");
    }


    @GetMapping("/deleteGroupMember")
    @ApiOperation("踢人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群 id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "accounts", value = "删除用户", dataType = "String", dataTypeClass = String.class)
    })
    public Result deleteGroupMember(String groupId, String accounts) {
        String result = tencentCloudImUtil.deleteGroupMember(groupId, accounts);
        return new Result().ok(result);
    }

}